#include <QtGui>

#include "uihandler.h"

UiHandler::UiHandler(QTextEdit *textEdit, QString fileName)
{
	txtEdit = textEdit;
	macroName = fileName.toUpper() + "_H";
	level = 0;
}

bool UiHandler::startElement(const QString & /* namespaceURI */,
                               const QString & /* localName */,
                               const QString &qName,
                               const QXmlAttributes &attributes)
{
    if (qName == "ui") {
        QString version = attributes.value("version");
        if (!version.isEmpty() && version != "4.0") {
            errorStr = QObject::tr("The file is not an Qt version 4.x file.");
            return false;
        }
		else {
			txtEdit->clear();
			cursor = txtEdit->textCursor();
			cursor.insertText("// Generated by sax parser test.\n\n");
			cursor.insertText("#ifndef " + macroName + "\n");
			cursor.insertText("#define " + macroName + "\n\n");
		}
    } else if (qName == "widget") {
		classAttr = attributes.value("class");
		nameAttr = attributes.value("name");
		level++;
		if(level == 1) {
			topWidget = nameAttr;
			cursor.insertText("class " + nameAttr + " : public " + classAttr + "\n");
			cursor.insertText("{\n");
			cursor.insertText("public:\n");
 			constructorText = "\n\t" + nameAttr + "() {\n";
		}
		else {
			cursor.insertText("\t" + classAttr + " *" + nameAttr + ";\n");
			constructorText += "\t\t" + nameAttr + " = new " + classAttr + ";\n";
		}
    } else if (qName == "property") {
        propName = attributes.value("name");
    }

    currentText.clear();
    return true;
}

bool UiHandler::endElement(const QString & /* namespaceURI */,
                             const QString & /* localName */,
                             const QString &qName)
{
    if (qName == "ui") {
			cursor.insertText("#endif\n");
	}
    else if (qName == "widget") {
		if(level == 1) {
			constructorText += "\t}\n";
			cursor.insertText(constructorText);
			cursor.insertText("};\n");
		}
		level--;
	}
    else if (qName == "string") {
		if(propName == "windowTitle") {
 			constructorText += "\t\tsetWindowTitle(tr(\"" +currentText + "\"));\n";
		}
		else if(propName == "text") { // QComboBox is incorrect.
 			constructorText += "\t\t" + nameAttr + "->setText(tr(\"" + currentText + "\"));\n";
		}
    }
    else if (qName == "x") {
		if(propName == "geometry") {
 			xTag = currentText;
		}	
	}
    else if (qName == "y") {
		if(propName == "geometry") {
 			yTag = currentText;
		}	
	}
    else if (qName == "width") {
		if(propName == "geometry") {
 			widthTag = currentText;
		}	
	}
    else if (qName == "height") {
		if(propName == "geometry") {
			if(level == 1) {
 				constructorText += "\t\tresize(" + widthTag + ", " + currentText + ");\n";
			}
			else {
				constructorText += "\t\t" + nameAttr + "->setGeometry(QRect(" + xTag + ", " + yTag + ", " + widthTag + ", " + currentText + "));\n";
			}
		}	
	}
    return true;
}

bool UiHandler::characters(const QString &str)
{
    currentText += str;
    return true;
}

bool UiHandler::fatalError(const QXmlParseException &exception)
{
    return false;
}

QString UiHandler::errorString() const
{
    return errorStr;
}
